package com.gitpractice.liwushuo.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gc.flashview.FlashView;
import com.gitpractice.liwushuo.R;
import com.gitpractice.liwushuo.bean.AdBannelBean;
import com.gitpractice.liwushuo.bean.SecondAdBean;
import com.gitpractice.liwushuo.bean.ThirdBean;
import com.gitpractice.liwushuo.utils.CircleImageView;
import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * Created by Administrator
 * time 2016/7/21.
 */

public class HomeItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mcontext;
    private List<ThirdBean.DataBean.ItemsBean> thirdBeen;
    private List<AdBannelBean.DataBean.BannersBean> mAdbanners;
    private List<SecondAdBean.DataBean.SecondaryBannersBean> secondbanners;
    private int index;
    private static final int TYPE1 = 1;
    private static final int TYPE2 = 2;

    public HomeItemAdapter(Context context, int index, List<ThirdBean.DataBean.ItemsBean> beans) {
        this.mcontext = context;
        this.index = index;
        this.thirdBeen = beans;
    }

    public HomeItemAdapter(Context mContext, int index, List<ThirdBean.DataBean.ItemsBean> itemsBeen,
                           List<AdBannelBean.DataBean.BannersBean> firstbanners,
                           List<SecondAdBean.DataBean.SecondaryBannersBean> secondbanners) {
        this.mcontext = mContext;
        this.index = index;
        this.thirdBeen = itemsBeen;
        this.mAdbanners =  firstbanners;
        this.secondbanners = secondbanners;
    }
    @Override
    public int getItemViewType(int position) {
        if (index == 0) {
            if (position == 0) {
                return TYPE1;
            } else {
                return TYPE2;
            }
        }
        return TYPE2;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        switch (viewType){
            case 1:
                 view = LayoutInflater.from(mcontext).inflate(R.layout.item_recycle, parent, false);
                return new FirstViewHolder(view);
            case 2:
                 view=  LayoutInflater.from(mcontext).inflate(R.layout.item_recycle2,parent,false);
                return new SecondHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int type = getItemViewType(position);
        switch(type){
            case TYPE1:
                FirstViewHolder fHolder = (FirstViewHolder) holder;
                List<String> mList = new ArrayList<>();
                Log.i("123",mList.size()+"");
                for (int i = 0; i < mAdbanners.size(); i++) {
                    mList.add(mAdbanners.get(i).getImage_url());

                }
                fHolder.fv.setImageUris(mList);
                fHolder.fv.setEffect(1);
                for (int i = 0; i < secondbanners.size(); i++) {
                    ImageView iv = new ImageView(mcontext);
                    //必须动态添加
                    ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(150, 150);
                    iv.setPadding(8, 8, 8, 8);
                    iv.setLayoutParams(lp);
                    Picasso.with(mcontext)
                            .load(secondbanners.get(i).getImage_url())
                            .config(Bitmap.Config.RGB_565)
                            .into(iv);
                    fHolder.lv.addView(iv);
                }
                SimpleDateFormat format = new SimpleDateFormat("—— MM月dd日 E ——");
                fHolder.currentTime.setText(format.format(new Date(System.currentTimeMillis())));
                 break;

            case TYPE2:
                SecondHolder secondHolder= (SecondHolder) holder;
                ThirdBean.DataBean.ItemsBean bean;
                if (index == 0) {
                    //防止画到最后crash掉
                    bean=thirdBeen.get(position-1);
                }else{
                    bean=thirdBeen.get(position);
                }
                //
                secondHolder.name.setText(bean.getAuthor().getNickname());
                secondHolder.small.setText(bean.getColumn().getCategory());
                secondHolder.tatle.setText(bean.getColumn().getTitle());
                Log.i("123",bean.getColumn().getTitle());
                secondHolder.des.setText(bean.getTitle());
                Log.i("1",bean.getTitle());
                secondHolder.count.setText(bean.getLikes_count()+"");
                Log.i("12",bean.getLikes_count()+"");
                Picasso.with(mcontext)

                        .load(bean.getAuthor().getAvatar_url())
                        .placeholder(R.mipmap.placeholder)
                        .config(Bitmap.Config.RGB_565)
                        .into(secondHolder.circleImageView);

                Picasso.with(mcontext)
                        .load(bean.getColumn().getBanner_image_url())
                        .placeholder(R.mipmap.placeholder)
                        .config(Bitmap.Config.RGB_565)
                        .into(secondHolder.imageView);

                //setListener(secondHolder);
                break;

            default:
                 break;
        }

    }

   /* private void setListener(SecondHolder secondHolder) {
        secondHolder.imageView

    }*/

    public void setList(List<ThirdBean.DataBean.ItemsBean> list) {
        this.thirdBeen = list;
        notifyDataSetChanged();
    }
    public void clearData(List<ThirdBean.DataBean.ItemsBean> list){
            this.thirdBeen=list;
            list.clear();
        notifyDataSetChanged();
    }
    public void clearData(List<AdBannelBean.DataBean.BannersBean> mAdbanners, List<SecondAdBean.DataBean.SecondaryBannersBean> secondbanners, List<ThirdBean.DataBean.ItemsBean> thirdBeen){
        this.mAdbanners = mAdbanners;
        this.secondbanners = secondbanners;
        this.thirdBeen = thirdBeen;
        //清楚数据
        mAdbanners.clear();
        secondbanners.clear();
        thirdBeen.clear();
        notifyDataSetChanged();
    }

    public void setList(List<AdBannelBean.DataBean.BannersBean> mAdbanners, List<SecondAdBean.DataBean.SecondaryBannersBean> secondbanners, List<ThirdBean.DataBean.ItemsBean> thirdBeen) {
        this.mAdbanners = mAdbanners;
        this.secondbanners = secondbanners;
        this.thirdBeen = thirdBeen;
        notifyDataSetChanged();
    }


    class FirstViewHolder extends RecyclerView.ViewHolder {

        private FlashView fv;
        private LinearLayout lv;
        private TextView currentTime;

        public FirstViewHolder(View v) {
            super(v);
            fv= (FlashView) v.findViewById(R.id.iv_re);
            lv= (LinearLayout) v.findViewById(R.id.content);
            currentTime= (TextView) v.findViewById(R.id.time);

        }
    }



    @Override
    public int getItemCount() {
        if (index != 0) {
            return thirdBeen == null ? 0 : thirdBeen.size();
        } else {
            return thirdBeen == null ? 0 : thirdBeen.size() + 1;
        }
    }
    public class SecondHolder extends RecyclerView.ViewHolder {
            TextView small;
            TextView name;
            TextView tatle;
            TextView count;
            CircleImageView circleImageView;
            ImageView imageView;
        ImageView likeimageView;
            TextView des;
        public SecondHolder(View itemView) {
            super(itemView);
            count= (TextView) itemView.findViewById(R.id.count);
            small= (TextView) itemView.findViewById(R.id.small);
            name= (TextView) itemView.findViewById(R.id.name);
            tatle= (TextView) itemView.findViewById(R.id.tatle);
            count= (TextView) itemView.findViewById(R.id.count);
            likeimageView= (ImageView) itemView.findViewById(R.id.favate);
            circleImageView= (CircleImageView) itemView.findViewById(R.id.cricleimage);
            imageView= (ImageView) itemView.findViewById(R.id.image);
            des= (TextView) itemView.findViewById(R.id.des);
        }
    }
}
